﻿using AutoMapper;
using AutoMapper.Execution;
using B.S.BsicData.Api.Read.Application.Command;
using B.S.BsicData.Api.Read.DTO;
using B.S.BsicData.Domain;
using B.S.BsicData.ErrorCode.Entity;
using B.S.BsicData.Infartrans;
using MediatR;

namespace B.S.BsicData.Api.Writer.Application.CommandHandler
{
    public class QueryWhouseCommandHandler : IRequestHandler<QueryWhouseCommand, ApiPaging<WHouseLocaDTO>>
    {
        private readonly IBaseReository<WHouseModeles> whRepo;
        private readonly IBaseReository<LocationModeles> loRepo;
        private readonly IBaseReository<WHouseLocationModel> wlRepo;
        private readonly IMapper mapper;

        public QueryWhouseCommandHandler(IBaseReository<WHouseModeles> whRepo, IBaseReository<LocationModeles> loRepo = null, IBaseReository<WHouseLocationModel> wlRepo = null, IMapper mapper = null)
        {
            this.whRepo = whRepo;
            this.loRepo = loRepo;
            this.wlRepo = wlRepo;
            this.mapper = mapper;
        }

        public async Task<ApiPaging<WHouseLocaDTO>> Handle(QueryWhouseCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<WHouseLocaDTO> result = new ApiPaging<WHouseLocaDTO>();
            var model =from a in whRepo.GetAll()
                join b in wlRepo.GetAll() on a.WhouseID equals b.WhouseID
                join c in loRepo.GetAll() on b.LocationID equals c.LocationID
                select new WHouseLocaDTO
                {
                    WhouseID=a.WhouseID,
                    WhouseNum=a.WhouseNum,
                    WhouseName=a.WhouseName,
                    Capacity=a.Capacity,
                    DementperId=a.DementperId,
                    Manager=a.Manager,
                    Phone=a.Phone,
                    WHouseAddr =a.WHouseAddr,
                    Sort=a.Sort,
                    State=a.State,
                    Lock=a.Lock,
                    Rember=a.Rember,
                    LocationID=c.LocationID,
                    LocaTionNum=c.LocaTionNum,
                    LocaTionName=c.LocaTionName,
                    LocaTionCapacity=c.LocaTionCapacity,
                    LocaTionManage=c.LocaTionManage,
                    LocaTionCStatus=c.LocaTionCStatus,
                    LocaTionSort=c.LocaTionSort,
                    LocaTionRember=c.LocaTionRember
                };
            if (!string.IsNullOrEmpty(request.WhouseNum))
            {
                model = model.Where(p => p.WhouseNum.Contains(request.WhouseNum));
            }
            if (request.Manager!=null)
            {
                model = model.Where(p => p.Manager==request.Manager);
            }
            if (request.State==false)
            {
                model = model.Where(p => p.State==request.State);
            }
            if (request.State == true)
            {
                model = model.Where(p => p.State == request.State);
            }
            result.TotalCount = model.Count();
            result.TotalPage = (int)Math.Ceiling((result.TotalCount*1.0)/ request.Pagesize);
            var list=model.OrderByDescending(p=>p.WhouseID).Skip((request.Pageindex-1)*request.Pagesize).Take(request.Pagesize).ToList();
            result.Code = ApiENums.成功;
            result.Message = "成功";
            result.list = mapper.Map<List<WHouseLocaDTO>>(list);
            return await Task.FromResult(result);

        }
    }
}
